import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout'

Vue.use(VueRouter)

export const constantRoutes = [
  {
    path: '',
    redirect: '/login',
    hidden: true,
  },
  {
    path: '/index',
    component: Layout,
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/system/user/user'),
        meta: { requireAuth: true }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/system/role/role'),
        meta: { requireAuth: true }
      },
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/system/menu/menu'),
        meta: { requireAuth: true }
      },
      {
        path: 'dept',
        name: 'dept',
        component: () => import('@/views/system/dept/dept'),
        meta: { requireAuth: true }
      },
      {
        path: 'myProject',
        name: 'myProject',
        component: () => import('@/views/project/MyProject'),
        meta: { requireAuth: true }
      },
      {
        path: 'recycle',
        name: 'recycle',
        component: () => import('@/views/project/Recycle'),
        meta: { requireAuth: true }
      },
      {
        path: 'course',
        name: 'course',
        component: () => import('@/views/course/index'),
        meta: { requireAuth: true }
      },
      {
        path: 'myCourse',
        name: 'myCourse',
        component: () => import('@/views/course/MyCourse'),
        meta: { requireAuth: true }
      },
      {
        path: 'course/detail',
        name: 'detail',
        component: () => import('@/views/course/detail'),
        meta: { requireAuth: true }
      },
      {
        path: 'courseProject',
        name: 'courseProject',
        component: () => import('@/views/course/courseProject'),
      },
      {
        path: 'category',
        name: 'category',
        component: () => import('@/views/template/category'),
      },
      {
        path: 'personalTemplate',
        name: 'personalTemplate',
        component: () => import('@/views/template/PersonalTemplate'),
        meta: { requireAuth: true }
      },
      {
        path: 'templateHandle',
        name: 'templateHandle',
        component: () => import('@/views/template/templateHandle'),
        meta: { requireAuth: true }
      }
    ]
  },

  {
    path: '/edit',
    name: 'edit',
    component: () => import('@/views/project/EditForm'),
    meta: { requireAuth: true }
  },
  {
    path: '/templateEdit',
    name: 'templateEdit',
    component: () => import('@/views/template/TemplateEdit'),
    meta: { requireAuth: true }
  },
  // {
  //   path: '/Index2',
  //   name: 'Index',
  //   component: () => import( '@/views/home/Index'),
  //   meta: { requireAuth: true },
  //   children: [
  //     {
  //       path: 'start',
  //       name: 'start',
  //       component: () => import('@/views/home/Start'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'edit',
  //       name: 'edit',
  //       component: () => import('@/views/project/EditForm'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'myProject',
  //       name: 'myProject',
  //       component: () => import('@/views/project/MyProject'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'recycle',
  //       name: 'recycle',
  //       component: () => import('@/views/project/Recycle'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'course',
  //       name: 'course',
  //       component: () => import('@/views/course/index'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'myCourse',
  //       name: 'myCourse',
  //       component: () => import('@/views/course/MyCourse'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'courseProject',
  //       name: 'courseProject',
  //       component: () => import('@/views/course/courseProject'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'course/detail',
  //       name: 'detail',
  //       component: () => import('@/views/course/detail'),
  //       meta: { requireAuth: true }
  //     },
  //     {
  //       path: 'message',
  //       name: 'message',
  //       component: () => import('@/views/course/message'),
  //       meta: { requireAuth: true }
  //     }
  //   ]
  // },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/account/Login')
  },
  {
    path: '/preview',
    meta: { requireAuth: false },
    component: () => import('@/views/project/form/skin/ProjectForm')
  },
  {
    path: '/write',
    meta: { requireAuth: false },
    component: () => import('@/views/project/form/Write')
  }
]

const creatRouter = () => new VueRouter({
  mode: 'history',
  routes: constantRoutes
})

const  router = creatRouter()

router.beforeEach((to, from, next) => {
  if (to.path === '/doLogin') {
    next()
  } else {
    const token = localStorage.getItem('Authorization')
    if (token === 'null' || token === '' ) {
      next('/doLogin')
    } else {
      next()
    }
  }
})


export default router
